//
//  MainViewController.m
//  ALFashion
//
//  Created by dengzhicheng on 15/6/13.
//  Copyright (c) 2015年 dengzhicheng. All rights reserved.
//

#import "MainViewController.h"
#import "MenuTableViewCell.h"
#import "MenuView.h"
#import <AFNetworking.h>
#import "MainModel.h"
#import "MainTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "MainDetailViewController.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface MainViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *table;
@property (strong, nonatomic) MenuView *menuView;

@end

@implementation MainViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.tableArray = [NSMutableArray array];
    }
    return self;
}

- (void)loadView
{
    [super loadView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.translucent = NO;
    
    [self setNavigation];
    [self starConnect];
    
}


// 设置导航栏
- (void)setNavigation
{
    
    // 打印所有字体
//    NSArray *familyNames = [UIFont familyNames];
//    
//    for( NSString *familyName in familyNames ){
//        
//        NSLog( @"Family: %s \n", [familyName UTF8String] );
//        
//        NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
//        
//        for( NSString *fontName in fontNames ){
//            
//            NSLog( @"\tFont: %s \n", [fontName UTF8String] );
//            
//        }
//        
//    }
    
    
    // 自定义view, 添加到导航栏的titleView上
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:titleView.frame];
    titleLabel.text = @"ALFashion";
    titleLabel.font = [UIFont fontWithName:@"huxiaobo-gdh" size:30];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:titleLabel];
    self.navigationItem.titleView = titleView;
    
    // 自定义button, 添加到导航栏的leftButtonItem上
    UIButton *menuButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 20, 20)];
    [menuButton setImage:[UIImage imageNamed:@"menu.png"] forState:UIControlStateNormal];
    [menuButton setImage:[UIImage imageNamed:@"menuSelected.png"] forState:UIControlStateHighlighted];
    [menuButton addTarget:self action:@selector(menuButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuButton];
}

// menu 按钮点击事件
- (void)menuButtonClicked:(UIButton *)button
{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MenuView" object:@(YES)];
    
}

- (void)starConnect
{
    NSString *url = @"http://c.m.163.com/nc/article/list/T1414463111571/0-10.html";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSData *data = operation.responseData;
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *array = [bigDic objectForKey:@"T1414463111571"];
        for (NSDictionary *dic in array) {
            MainModel *model = [[MainModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.tableArray addObject:model];
        }
        [self createView];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error = %@", error);
        
    }];
}


- (void)createView
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.rowHeight = 240;
    [self.view addSubview:self.table];
    
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    [scroll setBackgroundColor:[UIColor lightGrayColor]];
    [self.table setTableHeaderView:scroll];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[MainTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        
    }
    MainModel *model = [[MainModel alloc] init];
    model = [self.tableArray objectAtIndex:indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:[UIImage imageNamed:@"占位图.jpg"] options:SDWebImageRetryFailed];
    cell.titleLabel.text = model.title;
    NSString *str = model.ptime;
    NSArray *array = [str componentsSeparatedByString:@" "];
    cell.dateLabel.text = [array firstObject];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"------------------------");
    NSLog(@"indexPath.row = %ld", indexPath.row);
    
    MainDetailViewController *detail = [[MainDetailViewController alloc] init];
    MainModel *model = [[MainModel alloc] init];
    model = [self.tableArray objectAtIndex:indexPath.row];
//    NSArray *array = [model.docid componentsSeparatedByString:@"_"];
//    detail.docid = [array firstObject];
    detail.docid = model.docid;
    
    [self.navigationController pushViewController:detail animated:YES];
    detail.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}





@end
